import {
	ajax as commonAjax,
	getUserInfo
} from '@/common/common.js'
import {
	bpmUrl
} from "@/common/config.js"
let user = getUserInfo()
let paramas = {
	headersMap: {
		sysid: "bpm",
		syspwd: "pass",
		userid: user.bpmUid
	}
}
// js封装所需
function ajax(opt) {
	return new Promise((resolve, reject) => {
		let complete = (complete) => {
			if (opt.complete) {
				opt.complete(complete)
			}
			resolve(complete)
		}
		try {
			commonAjax({ ...opt,
				complete
			})
		} catch (e) {
			reject()
		}
	})
}
//初始赋值
function formonload(list, formData, bussiness) {
	list.forEach(item => {
		if (['staff_name', 'dept'].indexOf(item.name) != -1) {
			item.disabled = true
		}
	})
	return list
}

async function staffNoChange(queryString, formData, list) {
	console.log("item:", queryString)
	await ajax({
		url: '/bpm/get',
		method: 'POST',
		data: {
			...paramas,			
			paramasMap: {
				staff_no: queryString,
			},
			url: bpmUrl + '/rest/appform/R_hra/R_hra_P019'
		},
		success: res => {
		}
	})
	// 根据staff_no 赋值
	await ajax({
		url: '/bpm/get',
		method: 'POST',
		data: {
			...paramas,
			url: bpmUrl + '/rest/appform/R_hra/R_hra_B008'
		},
		success: res => {
			// todo: 赋值
		}
	})
	// 从申请表中获取员工个人信息
	await ajax({
		url: '/bpm/get',
		method: 'POST',
		data: {
			...paramas,
			url: bpmUrl + '/rest/appform/R_hra/R_hra_B014'
		},
		success: res => {
			// todo: 赋值 email、address、email
		}
	})
}

function radio11Change(item,formData,list) {
	if(formData.radio11 === 'yes') {
		list.forEach(item => {
			if (['radio11Detail'].indexOf(item.name) != -1) {
				item.hidden = '0'
			}
		})
	} else {
		list.forEach(item => {
			if (['radio11Detail'].indexOf(item.name) != -1) {
				item.hidden = '1'
			}
		})
	}
}

function formonsubmit(list, formData, bussiness) {
	let flag = true
	if(!formData.Subject || formData.Subject==='离职交接流程') {
		let processname = bussiness.processname
		formData.Subject = formData.staff_name + processname
	}
	return flag;
}

export default {
	formonload,
	staffNoChange,
	radio11Change,
	formonsubmit,
}
